<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/7/18
 * Time: 17:35
 * Created by Created by Panxsoft.
 */

namespace App\Models\Panx;

use App\Models\Admin\StoreAdmin;
use App\Models\Concerns\ScopeFilterable;
use App\Models\OpenWechat\Authorizer;
use App\Models\OpenWechat\WechatUser;
use App\Models\Store\SkinRecord;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class SkinRecordBonus
 *
 * @property int id
 * @property int $pay_type
 * @property float $amount
 * @property Carbon $created_at
 * @property int $bonus_status
 * @property string bonusable_type
 * @property array $data
 *
 * @property-read SkinRecord $skinRecord
 * @property-read StoreAdmin|WechatUser $bonusable
 *
 * @mixin Builder
 */
class SkinRecordBonus extends Model
{
    use ScopeFilterable;

    protected $table = 'skin_record_bonuses';

    protected $guarded = [];

    const BONUS_STATUS_SUCCESS = 1; // 已发放
    const BONUS_STATUS_UNSENT = 0; // 未发放
    const BONUS_STATUS_FAILED = 2; // 发放失败

    const BONUS_WX_PAY_TYPE = 1;   // 微信发红包
    const BONUS_ALI_PAY_TYPE = 2;  // 支付宝发红包
    const BONUS_NO_GRANT_PAY_TYPE = 0; // 已抽奖，金额为0

    const MAX_BONUS_AMOUNT = 50000;  // 红包活动总预算金额

    protected $casts = [
        'pay_type'        => 'int',
        'amount'          => 'float',
        'bonus_status'    => 'int',
        'data'            => 'json',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function bonusable()
    {
        return $this->morphTo('bonusable','bonusable_type','bonusable_id','id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|skinRecord
     */
    public function skinRecord()
    {
        return $this->belongsTo(SkinRecord::class);
    }

    /**
     * @return Authorizer
     */
    public function getTemplateNoticeAuthor(): ?Authorizer
    {
        return $this->skinRecord->store->customer->authorizer ?? null;
    }

    public function isUseWxPay(): bool
    {
        return $this->pay_type === SkinRecordBonus::BONUS_WX_PAY_TYPE;
    }

    public function isUseAliPay(): bool
    {
        return $this->pay_type === SkinRecordBonus::BONUS_ALI_PAY_TYPE;
    }

    public function isToStoreAdmin(): bool
    {
        return $this->bonusable_type === class_basename(StoreAdmin::class);
    }

    public function isToWechatUser(): bool
    {
        return $this->bonusable_type === class_basename(WechatUser::class);
    }

    public function isGrantSucceed(): bool
    {
        return $this->bonus_status === self::BONUS_STATUS_SUCCESS;
    }

    public function isGrantFailed(): bool
    {
        return $this->bonus_status === self::BONUS_STATUS_FAILED;
    }

    public function isGrantUnsent(): bool
    {
        return $this->bonus_status === self::BONUS_STATUS_UNSENT;
    }

    /**
     * @return bool
     */
    public function isZeroAmount(): bool
    {
        return $this->amount <= 0; // amount is float don't use ===
    }

    public function getWxTransferPayDescription(): string
    {
        $store = $this->skinRecord->store->name ?? '店铺未知';

        if ($this->isToStoreAdmin()) {
            $name = '店员:' . ($this->bonusable->wechatUser->name ?? '未知');
        } else {
            $name = '消费者:' . ($this->bonusable->name ?? '未知');
        }

        $desc = "红包-$store-$name";

        return $desc;
    }
}